package com.jyserver.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;


/**
 * @author: Seven.wk
 * @description: 辅助工具类
 * @create: 2018/07/04
 */
public class Utils {

    /**
     * 向目的URL发送post请求
     *
     * @param url    目的url
     * @param params 发送的参数
     * @return ResultVO
     */
    public static ResultVO sendPostRequest(String url, MultiValueMap<String, String> params) {
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<ResultVO> response = client.postForEntity(url, requestEntity, ResultVO.class);
        return response.getBody();
    }

    public static String sendPostRequestS(String url, MultiValueMap<String, String> params) {
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<String> response = client.postForEntity(url, requestEntity, String.class);
        return response.getBody();
    }

    /**
     * @Description :返回json结果集
     */
    public static Object json(Object data, Boolean status) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", data);
        jsonObject.put("status", status);
        return JSON.toJSON(jsonObject);
    }

    /**
     * @Description :返回json结果集
     */
    public static Object json(Object data, Boolean status, String message) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", data);
        jsonObject.put("status", status);
        jsonObject.put("message", message);
        return JSON.toJSON(jsonObject);
    }


    /**
     * @Description :返回json提示信息
     */
    public static Object json(Boolean status, String message) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", status);
        jsonObject.put("message", message);
        return JSON.toJSON(jsonObject);
    }


    /**
     * @Description :返回json结果集
     */
    public static Object json(Object data, Boolean status, Integer Limit, Long count, Integer offest, String message) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", data);
        jsonObject.put("status", status);
        jsonObject.put("limit", Limit);
        jsonObject.put("count", count);
        jsonObject.put("offset", offest);
        jsonObject.put("message", message);
        return JSON.toJSON(jsonObject);
    }

    /**
     * @Description :返回UUid
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * @Description :返回UUid排除斜杠
     */
    public static String getUUIDnotg() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-", "");
    }

    /**
     * @Description :根据字符数返回UUid
     */
    public static String getUUID(int number) {
        boolean t = true;
        String uuid = "";
        while (t) {
            uuid = uuid + UUID.randomUUID().toString();
            if (uuid.length() > number) {
                uuid = uuid.substring(0, number - 1);
                return uuid;
            }
        }
        return uuid;
    }

    /**
     * @Description :根据字符数返回UUid
     */
    public static String getUUIDnotg(int number) {
        boolean t = true;
        String uuid = "";
        while (t) {
            uuid = uuid + getUUIDnotg();
            if (uuid.length() > number) {
                uuid = uuid.substring(0, number - 1);
                return uuid;
            }
        }
        return uuid;
    }
}